<?php
/**
 * Created by PhpStorm
 * Author: CodeShuHao
 * Motto: 我用双手成就你的梦想
 * Date: 2025/6/18
 */
namespace App\Http\Controllers\Admin\Auth;
use App\Http\Controllers\Admin\Controller;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
class UploadController extends Controller{
	public function store(Request $request)
	{
		$file = $request->file('image');

		$validated = $request->validate([
			'image' => 'required|image|mimes:jpeg,png,jpg|max:2048'
		]);
		$date = date('Ymd');
		$filename = $date.'/'.time().'_'.uniqid().'.'.$file->extension();
		$path = $file->storeAs('uploads', $filename, 'public');
        $filePath=public_path('uploads/'.$date);
        if(!file_exists($filePath)){
             mkdir($filePath, 0777, true);
        }
		// 生成缩略图
		$thumb = Image::make($file)
			->resize(300, null, function($constraint){
				$constraint->aspectRatio();
			})->save(public_path('uploads/'.$filename));

        return response()->json([
            'data'=>[
                'url' => $path,
                'thumb' => asset("/uploads/$filename"),
            ],
            'code'=>200,
            'msg'=>'成功'
        ]);
	}
}
